// Perceptron.cpp : Defines the entry point for the console application.
//

#include "Root.h"

#include <stdio.h>
#include <tchar.h>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>

using std::cout;
using std::cin;
using std::endl;
using std::ifstream;
using std::ofstream;

vector<double> loadTrainData(const char* fileName)
{
	ifstream stream;
	vector<double> data;
	double value;

	stream.open(fileName);

	while (!stream.eof())
	{
		stream >> value;

		if (!stream.eof())
		{
			data.push_back(value);
		}
	}

	return data;
}

void saveTrainData(const char* fileName, vector<double> data)
{
	ofstream stream;
	
	stream.open(fileName);

	if (stream.is_open())
	{
		for (unsigned int i = 0; i < data.size(); i++)
		{
			stream << data[i] << endl;
		}

		stream.close();
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	srand((unsigned int)time(NULL));
	ofstream streamResults;
	ofstream streamTrainData;

	Root root;
	unsigned int power;
	double value = 1.0;

	vector<double> trainData;

	cout << "Podaj stopien pierwiastka: "; cin >> power;

	//Generowanie i zapis danych treningowych
	//trainData = Root::GenerateTrainData(power, 50);
	trainData = loadTrainData("..\\Data\\train.txt");
	root.SetTrainData(trainData);

	saveTrainData("..\\Data\\train.txt", trainData);

	//Nauka sieci
	cout << "Nauczanie sieci..." << endl;

	streamResults.open("..\\Results\\results.txt");

	for (unsigned int i = 20; i >= 1; i--)
	{
		cout << "Dla " << i << " neuronow w warstwie ukrytej" << endl;

		root.Learn(
			power,
			0.005,
			0.01,
			1000,
			i);

		cout << endl;

		streamResults << i
			<< " " << root.GetNumPeriods()
			<< " " << root.CountError()
			<< " " << root.GetLearningTime()
			<< endl;
	}
	
	streamResults.close();

	//Wypisanie wynikow nauki
	cout << endl;
	cout << "Siec nauczona" << endl;
	cout << "Blad sredniokwadratowy: " << root.CountError() << endl;
	cout << "Czas nauki: " << root.GetLearningTime() << endl;
	cout << "Ilosc epok: " << root.GetNumPeriods() << endl;
	cout << endl;

	//Obliczanie wartosci
	while (value != 0.0)
	{
		cout << "Podaj wartosc: "; cin >> value;

		if (value != 0.0)
		{
			cout << "Wartosc pierwiastka: " << root.Count(value) << endl;
		}
	}

	system("pause");

	return 0;
}

